package com.example.demo.demos.web;


import org.apache.commons.codec.binary.Base64;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.file.Files;
import java.nio.file.Paths;

@RestController
@RequestMapping("/api")
public class FileAndJsonController {

    @GetMapping("/file-and-json")
    public ResponseEntity<FileAndJsonResponse> getFileAndJson() throws Exception {
        // 构建JSON数据
        MyJsonData jsonData = new MyJsonData("example", 123);

        // 读取图像文件内容（假设文件路径为 "/path/to/image.png"）
        byte[] fileContent = Files.readAllBytes(Paths.get("C:\\Users\\c\\Desktop\\fpnn.png"));
        String fileName = "image.png";

        // 将文件内容编码为Base64
        String fileContentBase64 = Base64.encodeBase64String(fileContent);

        // 创建自定义响应体对象
        FileAndJsonResponse response = new FileAndJsonResponse();
        response.setFileContentBase64(fileContentBase64);
        response.setFileName(fileName);
        response.setJsonData(jsonData);

        return ResponseEntity.ok(response);
    }
}
